package com.zj.syzs.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.zj.shouyou.R;
import com.zj.syzs.bean.GiftBean;
import com.zj.syzs.utils.ImageDownLoader;

import java.util.List;

/**
 * Created by ZJ on 2015/6/16 0016.
 */
public class Mobile_Web_Adapter extends AppAdapter<GiftBean> {
    private ImageDownLoader task;
    private DisplayImageOptions options;

    public Mobile_Web_Adapter(List<GiftBean> list, Context context) {
        super(list, context);
        task = new ImageDownLoader(context);
        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context));
        DisplayImageOptions.Builder builder = new DisplayImageOptions.Builder();
        builder.showImageOnLoading(R.mipmap.ic_launcher);
        builder.showImageOnFail(R.mipmap.ic_launcher);
        builder.cacheInMemory(true).cacheOnDisk(true);
        builder.displayer(new RoundedBitmapDisplayer(20));
        options = builder.build();
    }

    @Override
    public View createView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        GiftBean giftBean = list.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_listview_gift, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.gamename.setText(giftBean.getGamename());
        holder.conetnt.setText(giftBean.getContent());
        holder.remain.setText(giftBean.getRemain());
        if (!giftBean.getRemain().equals("0")) {
            if (giftBean.getConsume().equals("0.0")) {
                holder.gift_btn.setText("免费领取");
            } else {
                holder.gift_btn.setText(giftBean.getConsume() + "Q币");
            }
        } else {
            holder.gift_btn.setText("已领完");
            holder.gift_btn.setClickable(false);
        }

        ImageLoader.getInstance().displayImage(list.get(position).getIcon(), holder.icon, options);

        //如果图片存在内存中,直接返回 bitmap对象
        Bitmap bitmap = task.downLoader(holder.icon, new ImageDownLoader.ImageLoaderlistener() {
            @Override
            public void onImageLoader(Bitmap bitmap, ImageView imageView) {
                imageView.setImageBitmap(bitmap);
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        ImageView icon;
        TextView gamename, remain, conetnt;
        Button gift_btn;

        public ViewHolder(View itemView) {
            icon = (ImageView) itemView.findViewById(R.id.gift_icon);
            gamename = (TextView) itemView.findViewById(R.id.gift_gamename);
            conetnt = (TextView) itemView.findViewById(R.id.gift_content);
            remain = (TextView) itemView.findViewById(R.id.gift_remain);
            gift_btn = (Button) itemView.findViewById(R.id.gift_btn);
        }
    }
}
